package ruifeng.zcj.dig.base.api;

import android.text.TextUtils;

import com.google.gson.Gson;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import okhttp3.MediaType;
import ruifeng.zcj.dig.groble.ConnUtils;
import ruifeng.zcj.dig.groble.Param;
import ruifeng.zcj.dig.ui.register.LoginActivity;
import ruifeng.zcj.dig.ui.register.bean.LoginUserBean;
import ruifeng.zcj.mybase.base.utils2.RandomUtil;
import ruifeng.zcj.mybase.base.utils2.TimeUtil;

/**
 * 网络请求业务层的封装业务
 * Created by Administrator on 2016/9/18 0018.
 */
public class OSChinaApi {
    /**
     * 登陆
     *
     * @param username
     * @param password
     * @param handler
     */
    public static void login(String username, String password,
                             RequestCallBack<String> handler) {
        RequestParams params = getHeadParams();

        params.addBodyParameter("username", username);
        params.addBodyParameter("password", password);


        String loginurl = "appservice/login.action";
        ApiHttpClient.post(loginurl, params, handler);


    }

    /**
     * 登陆
     *
     * @param username
     * @param password
     * @param handler
     */
    public static void login2(String username, String password,
                              StringCallback handler) {

        Map<String, String> params = getHeadParams2();

        params.put("username", username);
        params.put("password", password);

        OkHttpUtils
                .post()
                .url("http://kw.runfkj.com/appservice/login.action")
                .params(params)
                .build()
                .execute(handler);

    }

    public static HashMap getHeadParams2() {
        HashMap params = new HashMap();
        String timestamp = TimeUtil.getNowYMDHMSTime();
        String nonce = RandomUtil.create_nonce_str();
        params.put("signature", ConnUtils.getSignature(ConnUtils.token, timestamp, nonce));
        params.put("timestamp", timestamp);
        params.put("nonce", nonce);

        return params;
    }

    public static RequestParams getHeadParams() {
        RequestParams params = new RequestParams();
        String timestamp = TimeUtil.getNowYMDHMSTime();
        String nonce = RandomUtil.create_nonce_str();

        params.addBodyParameter("signature", ConnUtils.getSignature(ConnUtils.token, timestamp, nonce));
        params.addBodyParameter("timestamp", timestamp);
        params.addBodyParameter("nonce", nonce);
        return params;
    }

    /**
     * 上传图片文字视频
     *
     * @param
     * @param
     * @param handler
     */
    public static void upload(Map<String, String> name, ArrayList<File> files,
                              RequestCallBack<String> handler) {
        RequestParams params = getHeadParams();

        for (String key : name.keySet()) {
            params.addBodyParameter(key, name.get(key));
        }
        for (int i = 0; i < files.size(); i++) {
            params.addBodyParameter("1" + i, files.get(i));
        }


        String loginurl = "appservice/login.action";
        ApiHttpClient.post(loginurl, params, handler);
    }

    /**
     * 注册
     *
     * @param
     * @param
     * @param handler
     */
    public static void register(String mobile, String password,
                                RequestCallBack<String> handler) {
        RequestParams params = getHeadParams();

        params.addBodyParameter("mobile", mobile);
        params.addBodyParameter("password", password);

        String loginurl = "appservice/register.action";
        ApiHttpClient.post(loginurl, params, handler);
    }

    /**
     * 获取验证码
     *
     * @param
     * @param
     * @param handler
     */
    public static void getCode(String mobile, String type,
                               RequestCallBack<String> handler) {
        RequestParams params = getHeadParams();

        params.addBodyParameter("mobile", mobile);
        params.addBodyParameter("type", type);

        String loginurl = "appservice/getcode.action";
        ApiHttpClient.post(loginurl, params, handler);
    }


    /**
     * 获取帖子列表
     * <p/>
     * orderby	排序类型 0 时间倒序 1浏览量（可空）
     * section	二级版块中文缩称:如故障排除GZPC(可空）
     * city	城市(可选 ，二手车版块需要)
     * pageindex	页码
     * pagesize	页容量
     *
     * @param
     * @param
     * @param handler
     */
    public static void getpostingslist(String orderby, String city, String section, String pageindex, String pagesize,
                                       RequestCallBack<String> handler) {
        RequestParams params = getHeadParams();

        params.addBodyParameter("orderby", orderby);
//        params.addBodyParameter("userid", "1");

        params.addBodyParameter("city", city);
        params.addBodyParameter("section", section);
        params.addBodyParameter("pageindex", pageindex);
        params.addBodyParameter("pagesize", pagesize);

        String url = "appservice/getpostingslist.action";
        ApiHttpClient.post(url, params, handler);
    }

    /****
     * 获取商品的一级分类
     **/
    public static void getsuppliergoodstypelist(int category, String supplierid, RequestCallBack<String> handler) {
        String loginurl = null;
        RequestParams params = getHeadParams();
        switch (category) {
            case 0:
                loginurl = "appservice/getsuppliergoodstypelist.action";
                break;
            case 1:
                loginurl = "appservice/getsubtiergoodstypelist.action";
                params.addBodyParameter("supplierid", supplierid);
                break;
            case 2:
                loginurl = "appservice/getsumciergoodstypelist.action";
                if (!TextUtils.isEmpty(supplierid)) {
                    params.addBodyParameter("subtierid", supplierid);
                }
                break;
            default:
                break;
        }
        if (loginurl != null) {
            ApiHttpClient.post(loginurl, params, handler);
        }
    }


    /****
     * 七、获取商品列表
     ***/
    public static void getgoodslist(String sumcierid, int orderby, String userid, RequestCallBack<String> handler) {
        if (TextUtils.isEmpty(sumcierid) && TextUtils.isEmpty(userid)) return;
        String loginurl = null;
        RequestParams params = getHeadParams();
        params.addBodyParameter("sumcierid", sumcierid);// 三级分类ID
        switch (orderby) {
            case 0:
                params.addBodyParameter("orderby", "0");// 排序方式：0默认1销量2价格
                break;
            case 1:
                params.addBodyParameter("orderby", "1");
                break;
            case 2:
                params.addBodyParameter("orderby", "2");
                break;
            default:
                params.addBodyParameter("orderby", "0");
                break;
        }
        params.addBodyParameter("userid", userid);
        loginurl = "appservice/getgoodslist.action";
        ApiHttpClient.post(loginurl, params, handler);
    }


    /****
     * 七、获取商品详情
     ***/
    public static void getgoodsinfobyid(String goodsinfoid, String userid, RequestCallBack<String> handler) {
        if (TextUtils.isEmpty(goodsinfoid) && TextUtils.isEmpty(userid)) return;
        String loginurl = null;
        RequestParams params = getHeadParams();
        params.addBodyParameter("userid", userid);
        params.addBodyParameter("goodsinfoid", goodsinfoid);

        loginurl = "appservice/getgoodsinfobyid.action";
        ApiHttpClient.post(loginurl, params, handler);
    }

    /****
     * 七、提交订单
     * goodslist :订单信息
     ***/
    public static void submitorder(String totalprice, String userid,
                                   String goodslist, String consigneeaddress,
                                   String consigneearea, String consigneecity,
                                   String consigneeprovince, String consigneemobile,
                                   String consigneename, RequestCallBack<String> handler) {

        if (TextUtils.isEmpty(goodslist) && TextUtils.isEmpty(userid)) return;
        String loginurl = null;
        RequestParams params = getHeadParams();
        params.addBodyParameter("userid", userid);// 用户ID
        params.addBodyParameter("goodslist", goodslist); // 所有订单信息
        params.addBodyParameter("totalprice", totalprice); // 商品总价
        params.addBodyParameter("consigneeaddress", consigneeaddress);// 详细地址
        params.addBodyParameter("consigneearea", consigneearea);// 区
        params.addBodyParameter("consigneecity", consigneecity);// 市
        params.addBodyParameter("consigneeprovince", consigneeprovince);// 省
        params.addBodyParameter("consigneemobile", consigneemobile);// 收货人联系方式
        params.addBodyParameter("consigneename", consigneename);// 收货人姓名

        loginurl = "appservice/submitorder.action";
        ApiHttpClient.post(loginurl, params, handler);
    }

    /****
     * 获取帖子的详情
     **/
    public static void getpostingsdetail(String postingsid, RequestCallBack<String> handler) {
        String loginurl = null;
        RequestParams params = getHeadParams();
        loginurl = "appservice/getpostingsdetail.action";
        params.addBodyParameter("postingsid", postingsid);
        ApiHttpClient.post(loginurl, params, handler);
    }

    /****
     * 评论帖子
     * type	1贴子评论 2招聘求职评论
     * postingsid	帖子/招聘求职ID
     * userid	用户ID
     * comment	评论内容
     * type	1贴子评论 2招聘求职评论
     **/
    public static void sendcomment(String postingsid, String userid, String comment, String type, RequestCallBack<String> handler) {
        String loginurl = null;
        RequestParams params = getHeadParams();
        loginurl = "appservice/sendcomment.action";
        params.addBodyParameter("id", postingsid);
        params.addBodyParameter("userid", userid);
        params.addBodyParameter("comment", comment);
        params.addBodyParameter("type", type);
        ApiHttpClient.post(loginurl, params, handler);
    }

    /****
     * 发布帖子
     * <p/>
     * postingsname	帖子标题
     * postingscontent	帖子内容
     * firstsection	一级版块
     * cnfirstsection	一级版块中文缩称
     * secondsection	二级版块
     * cnsecondsection	二级版块中文缩称
     * fuserid	用户ID
     * istop	是否置顶 0否 1是
     * city	区域(可选，二手配件需要此参数)
     * file1	文件1
     * file2	文件2
     **/
    public static void sendpostings(String postingsname, String postingscontent,
                                    String firstsection, String cnfirstsection
            , String secondsection, String cnsecondsection,
                                    String fuserid, int istop,
                                    ArrayList<File> files,
                                    String city, RequestCallBack<String> handler) {
        String loginurl = null;
        RequestParams params = getHeadParams();
        loginurl = "appservice/sendpostings.action";
        params.addBodyParameter("postingsname", postingsname);
        params.addBodyParameter("postingscontent", postingscontent);
        params.addBodyParameter("firstsection", firstsection);
        params.addBodyParameter("cnfirstsection", cnfirstsection);
        params.addBodyParameter("secondsection", secondsection);
        params.addBodyParameter("cnsecondsection", cnsecondsection);
        params.addBodyParameter("city", city);
        params.addBodyParameter("fuserid", fuserid);
        switch (istop) {
            case 0:
                params.addBodyParameter("istop", "0");
                break;
            case 1:
                params.addBodyParameter("istop", "1");
                break;
            default:
                break;
        }
        for (int i = 0; i < files.size(); i++) {
            params.addBodyParameter("1" + i, files.get(i));
        }
        ApiHttpClient.post(loginurl, params, handler);
    }

}
